package com.kafka.valid;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.kafka.bean.User;
import com.kafka.common.Constant;
import com.kafka.common.String.StringValues;
import com.kafka.core.BaseProducer;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.CheckUtil;

public class UserValid extends BaseProducer{
	/**
	 * 校验用户注册
	 * @param message
	 * @param msgType
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validRegister(JSONObject params,String msgId,Integer validType) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 用户信息注册或修改
		try {
			//**************************必填字段校验*****************************************
			JSONObject validRequired = validRequired(params,result,msgId);
			if(validRequired!=null){
				return validRequired;
			}
			
			if(params.getString("passwd").isEmpty()){
				return failMsg(result,StringValues.PASSWORD_CAN_NOT_BE_EMPTY,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			//**************************跨平台校验校验*****************************************
			if(validType == Constant.VALID){
				JSONObject message = new JSONObject();
				message.put("datas", params);
				JSONObject userInfo = validUserWithPlatform(message);
				if (userInfo != null) {
					return failMsg(result,StringValues.USER_REGISTRATION_IN_OHER_CENTER,ResponseType.USER_EXIST.toString(),msgId);
				}
			}
			
			//**************************查询字段校验*****************************************
			//在注册检查uuid是否已存在
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) != null) {
				return failMsg(result,StringValues.UUID_IS_EXIST,ResponseType.UUID_EXIST.toString(),msgId);
			}
			
			//注册时仅校验登录名手机号是否存在
			query.clear();
			query.put("login_name", params.getString("loginname"));
			if (userDao.selectByCondition(query) != null) {
				return failMsg(result,StringValues.LOGIN_NAME_EXIST,ResponseType.LOGIN_NAME_EXIST.toString(),msgId);
			}
			
			if(!params.getString("agedness_card").isEmpty()){
				query.clear();
				query.put("agedness_card", params.getString("agedness_card"));
				if (!params.getString("agedness_card").isEmpty() && userDao.selectByCondition(query) != null) {
					return failMsg(result,StringValues.AGEDNESS_CARD_EXIST,ResponseType.AGEDNESS_CARD_EXIST.toString(),msgId);
				}
			}
			
			query.clear();
			query.put("mobile", params.getString("mobile"));
			if (userDao.selectByCondition(query) != null) {
				return failMsg(result,StringValues.MOBILE_EXIST,ResponseType.MOBILE_EXIST.toString(),msgId);
			}
			
			if(!params.getString("email").isEmpty()){
				query.clear();
				query.put("email", params.getString("email"));
				if (!params.getString("email").isEmpty() && userDao.selectByCondition(query) != null) {
					return failMsg(result,StringValues.EMAIL_EXIST,ResponseType.EMAIL_EXIST.toString(),msgId);
				}
			}
			
			query.clear();
			query.put("sid", params.getString("sid"));
			if (userDao.selectByCondition(query) != null) {
				return failMsg(result,StringValues.SID_EXIST,ResponseType.SID_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			//判断当操作是注册和更新的时候对字段值进行校验
			JSONObject validDataFormat = validDataFormat(params,result,msgId);
			if(validDataFormat!=null){
				return validDataFormat;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	/**
	 * 校验用户更新
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validUpdate(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 用户信息注册或修改
		try {
			//**************************必填字段校验*****************************************
			JSONObject validRequired = validRequired(params,result,msgId);
			if(validRequired!=null){
				return validRequired;
			}
			
			//**************************查询字段校验*****************************************
			//在更新检查操作的uuid是否存在
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_NOT_FOUND.toString(),msgId);
			}

			//更新时校验登录名和手机号是否存在且uuid是否与当前uuid相同
			query.clear();
			query.put("login_name", params.getString("loginname"));
			User user = userDao.selectByCondition(query);
			if (user != null && !user.getUuid().equals(params.getString("uuid"))) {
				return failMsg(result,StringValues.LOGIN_NAME_EXIST,ResponseType.LOGIN_NAME_EXIST.toString(),msgId);
			}

			if(!params.getString("agedness_card").isEmpty()){
				query.clear();
				query.put("agedness_card", params.getString("agedness_card"));
				user = userDao.selectByCondition(query);
				if (user != null && !user.getUuid().equals(params.getString("uuid"))) {
					return failMsg(result,StringValues.AGEDNESS_CARD_EXIST,ResponseType.AGEDNESS_CARD_EXIST.toString(),msgId);
				}
			}
			
			query.clear();
			query.put("mobile", params.getString("mobile"));
			user = userDao.selectByCondition(query);
			if (user != null && !user.getUuid().equals(params.getString("uuid"))) {
				return failMsg(result,StringValues.MOBILE_EXIST,ResponseType.MOBILE_EXIST.toString(),msgId);
			}
			
			if(!params.getString("email").isEmpty()){
				query.clear();
				query.put("email", params.getString("email"));
				user = userDao.selectByCondition(query);
				if (user != null && !user.getUuid().equals(params.getString("uuid")) ) {
					return failMsg(result,StringValues.EMAIL_EXIST,ResponseType.EMAIL_EXIST.toString(),msgId);
				}
			}
			
			query.clear();
			query.put("sid", params.getString("sid"));
			user = userDao.selectByCondition(query);
			if (user != null && !user.getUuid().equals(params.getString("uuid"))) {
				return failMsg(result,StringValues.SID_EXIST,ResponseType.SID_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			//判断当操作是注册和更新的时候对字段值进行校验
			JSONObject validDataFormat = validDataFormat(params,result,msgId);
			if(validDataFormat!=null){
				return validDataFormat;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	/**
	 * 校验必填字段
	 * @param params
	 * @param result
	 * @param msgId
	 * @return
	 */
	private static JSONObject validRequired(JSONObject params,JSONObject result,String msgId){
		try {
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("loginname").isEmpty()){
				return failMsg(result,StringValues.LOGIN_NAME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("true_name").isEmpty()){
				return failMsg(result,StringValues.TRUE_NAME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("mobile").isEmpty()){
				return failMsg(result,StringValues.MOBILE_EXIST_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			return null;
		}  catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
	}
	
	/**
	 * 校验注册/更新提交的数据格式
	 * @param params
	 * @param result
	 * @param msgId
	 * @return
	 */
	private static JSONObject validDataFormat(JSONObject params,JSONObject result,String msgId){
		try {
			if(!CheckUtil.checkStrLength(params.getString("uuid"))){
				return failMsg(result,StringValues.UUID_NOT_GREATER_THAN_32BIT,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("phone")) && !params.getString("phone").isEmpty()){
				return failMsg(result,StringValues.PHONE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isMobile(params.getString("mobile"))){
				return failMsg(result,StringValues.MOBILE_FORMAT_ERROR,ResponseType.MOBILE_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isEmail(params.getString("email")) && !params.getString("email").isEmpty()){
				return failMsg(result,StringValues.EMAIL_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isDate(params.getString("birthday")) && !params.getString("birthday").isEmpty()){
				return failMsg(result,StringValues.BIRTHDAY_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			String idCardValidate = CheckUtil.IDCardValidate(params.getString("sid"));
			if(!"".equals(idCardValidate)){
				return failMsg(result,idCardValidate,ResponseType.IDCARD_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("qq")) && !params.getString("qq").isEmpty()){
				return failMsg(result,StringValues.QQ_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("agedness_card")) && !params.getString("agedness_card").isEmpty()){
				return failMsg(result,StringValues.AGEDNESS_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("constellation")) && !params.getString("constellation").isEmpty()){
				return failMsg(result,StringValues.CONSTELLATION_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("user_class")) && !params.getString("user_class").isEmpty()){
				return failMsg(result,StringValues.USER_CLASS_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("politics")) && !params.getString("politics").isEmpty()){
				return failMsg(result,StringValues.POLITICS_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("area")) && !params.getString("area").isEmpty()){
				return failMsg(result,StringValues.AREA_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("zodiac")) && !params.getString("zodiac").isEmpty()){
				return failMsg(result,StringValues.ZODIAC_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("nation")) && !params.getString("nation").isEmpty()){
				return failMsg(result,StringValues.NATION_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
//				if(!CheckUtil.isNumber(params.getString("integral")) && !params.getString("integral").isEmpty()){
//					return failMsg(result,"积分的值必须是数字",ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
//				}
			
			if(!CheckUtil.isNumber(params.getString("service_class")) && !params.getString("service_class").isEmpty()){
				return failMsg(result,StringValues.SERVICE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("marriage")) && !params.getString("marriage").isEmpty()){
				return failMsg(result,StringValues.MARRIAGE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("province")) && !params.getString("province").isEmpty()){
				return failMsg(result,StringValues.PROVINCE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("nationality")) && !params.getString("nationality").isEmpty()){
				return failMsg(result,StringValues.NATIONALITY_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("city")) && !params.getString("city").isEmpty()){
				return failMsg(result,StringValues.CITY_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("source")) && !params.getString("source").isEmpty()){
				return failMsg(result,StringValues.SOURCE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("gender")) && !params.getString("gender").isEmpty()){
				return failMsg(result,StringValues.GENDER_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
	}
	
	/**
	 * 校验用户激活
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validActivate(JSONObject params, String msgId){
		JSONObject validServiceOperate = validServiceOperate(params, msgId);
		return validServiceOperate;
	}
	
	/**
	 * 校验用户黑名单
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validBlackList(JSONObject params,String msgId){
		JSONObject validServiceOperate = validServiceOperate(params, msgId);
		return validServiceOperate;
	}
	
	/**
	 * 校验用户注销
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validClose(JSONObject params,String msgId){
		JSONObject validServiceOperate = validServiceOperate(params, msgId);
		return validServiceOperate;
	}
	
	/**
	 * 校验用户激活/黑名单/注销 业务操作
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	private static JSONObject validServiceOperate(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 用户信息注册或修改
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,"uuid不能为空",ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}

			//**************************查询字段校验*****************************************
			//在注册检查uuid是否已存在
			query.clear();
			query.put("uuid", params.getString("uuid"));
			//在激活时检查操作的uuid是否存在
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,"uuid不存在",ResponseType.UUID_NOT_FOUND.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!CheckUtil.checkStrLength(params.getString("uuid"))){
				return failMsg(result,"UUID的长度不得大于36位！",ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			// 没有错误则返回null
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		
	}
}
